library(ProbBayes)

areas <- c(2,1,2,1,2)

spinner_plot(areas)

p_dist <- spinner_probs(areas)
p_dist



s_reg_A <- c(2,2,2,2)
s_reg_B <- c(4,1,1,2)
s_reg_C <- c(2,4,2)
s_reg_D <- c(1,3,3,1)

many_spinner_plots(list(s_reg_A, s_reg_B, s_reg_C, s_reg_D))

bayes_table <- data.frame(Model = c("Spinner A", "Spinner B", "Spinner C", "Spinner D"))

bayes_table$Prior <- rep(1/4, 4)

prob_plot(bayes_table)

bayes_table$Likelihood <- c(1/4,1/2,1/4,1/8)


bayes_table <- bayesian_crank(bayes_table)

prior_post_plot(bayes_table)


oneplay <- function(){
  flips <- sample(c("H", "T"), size=5, replace=TRUE, prob=c(0.5, 0.5))
  
  2 * sum(flips == "H") - 2 * sum(flips == "T")
}

G <- replicate(1000, oneplay())

G

mean(G)


die1 <- c(1,1,1,1,1,1) / 6
die2 <- c(1,1,4,4,1,1) / 12

rolls1 <- sample(1:6, prob=die1, size=1000, replace=TRUE)
rolls2 <- sample(1:6, prob=die2, size=1000, replace=TRUE)

c(mean(rolls1), sd(rolls1))
c(mean(rolls2), sd(rolls2))


x <- seq(-1, 10, length.out=100)

y1 <- dnorm(x, mean=mean(rolls1), sd=sd(rolls1))
y2 <- dnorm(x, mean=mean(rolls2), sd=sd(rolls2))

plot(x, y1, type="l", col="blue", lwd=2, ylim=c(0, max(y1,y2)*1.1), xlab="x", ylab="Density")
# plot(x, y2, type="l", col="red", lwd=2, add=TRUE)
lines(x, y2, color="red", lwd=2)



gm <- data.frame(x = 0:5) %>%
  mutate(
    P1 = dbinom(x, size=5, prob=0.3),
    P2 = dbinom(x, size=5, prob=0.5)
  ) 

gm

ggplot(gm, aes(x = x)) +
  geom_col(aes(y = P1), fill="blue", alpha=0.5) +
  geom_col(aes(y = P2), fill="red", alpha=0.5) +
  labs(title="Binomial Distributions", x="Number of Successes", y="Probability") +
  theme_minimal() +
  scale_y_continuous(labels=scales::percent)

hits <- rbinom(50, size=5, prob=0.3)

table(hits)

mean(hits)
sd(hits)

dnbinom(3, size=2, prob=0.372)

rnbinom(10, size=2, prob=0.372)

r <- 2
r_fail <- r - 1
p <- 0.372

gm <- data.frame(x= 2:9) %>%
  mutate(
    P = choose(x-1, r-1) * (p^r) * (1 - p)^(x-r),
    P2 = choose(x+r-1, x) * (p^r) * (1 - p)^x
  )

gm

ggplot(gm, aes(x = x, y = P)) +
  geom_col(fill="blue", alpha=0.5) +
  labs(title="Negative Binomial Distribution", x="Number of Trials", y="Probability") +
  theme_minimal() +
  scale_y_continuous(labels=scales::percent)


choose(5-1,1)* p**2*(1-p)**3
dnbinom(3,size=2, prob=p)
choose(3+2-1, 3)*p**2*(1-p)**3

choose(4,1)
choose(4,3)

choose(5,4)
choose(5,1)
choose(5,2)
choose(5,3)

233 / 632 * 360